package com.erpoa.service.impl;

import com.erpoa.annotation.DataBase;
import com.erpoa.constants.DataSourceNames;
import com.erpoa.entity.PackMessage;
import com.erpoa.mapper.BatchInspectInkMapper;
import com.erpoa.service.BatchInspectInkService;
import com.erpoa.utils.PDAException;
import com.erpoa.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author chenjimin
 * @Title: BatchInspectInkServiceImpl
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2020/8/2814:45
 */
@Service
public class BatchInspectInkServiceImpl implements BatchInspectInkService {
    private static final Logger logger = LoggerFactory.getLogger(BatchInspectInkServiceImpl.class);

    @Autowired
    private BatchInspectInkMapper batchInspectInkMapper;

    @Override
    @DataBase(DataSourceNames.DSX5)
    public List<Map> selectRecmatDetailByMachine(PackMessage packMessage) {
        if (StringUtil.isEmpty(packMessage.getUser().getsCode())) {
            throw new PDAException("未获取到用户信息，请重新登录！");
        }
        //先查询数据权限

        List<HashMap> ffactorycodes = this.batchInspectInkMapper.selectallcpgenbyhrno(packMessage.getUser().getsCode());
        if (StringUtil.isEmpty(ffactorycodes)) {
            throw new PDAException("当前用户：" + packMessage.getUser().getsCode() + "，未配置数据权限，请联系相关人员配置数据权限后再尝试！");
        }
        if (StringUtil.isNotEmpty(packMessage.getBarcode())) {
            String permission = ffactorycodes.get(0).get("TC_PGEN04").toString();
            //先过滤排程
            List<HashMap> fplanids = this.batchInspectInkMapper.selectDataWithfmachinecode(StringUtil.replaceBlank(packMessage.getBarcode()), permission, ffactorycodes);

            if (StringUtil.isEmpty(fplanids)) {
                throw new PDAException("当前用户：" + packMessage.getUser().getsCode() + "暂无该机台数据权限或无排程数据，请稍后重试！");
            }
            List<Map> detailByFplanids = batchInspectInkMapper.selectRecmatDetailByFplanids(fplanids);
            if (StringUtil.isNotEmpty(detailByFplanids)) {
                return detailByFplanids;
            } else {
                throw new PDAException("当前用户：" + packMessage.getUser().getsCode() + "暂无发料数据，请稍后重试！");
            }

        } else {
            throw new PDAException("当前用户：" + packMessage.getUser().getsCode() + "暂无数据，请稍后重试！");
        }
    }

    @Override
    @DataBase(DataSourceNames.DSX5)
    @Transactional()
    public String putDataLog(PackMessage packMessage) {
        Map param = new HashMap();
        param.put("uuid", UUID.randomUUID().toString());
        param.put("machine", packMessage.getMachine());
        param.put("messege", Boolean.valueOf(packMessage.getStatus()) ? "1" : "2");//提示信息1,墨水发料批次使用正常2,墨水发料批次与实物不一致
        param.put("fbatch", packMessage.getBarcode());
        param.put("hrno", packMessage.getUser().getsCode());
        try {
            param.put("ffactorycode", this.batchInspectInkMapper.selectallcpgenbyhrnofirst(packMessage.getUser().getsCode()));
            this.batchInspectInkMapper.insertScanLog(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new PDAException("未记录日志！");
        }

        return "已记录日志！";
    }
}
